use std::sync::Arc;

use parking_lot::ReentrantMutex;

use crate::{pools::HasPoolHandle, utils::NullStr};

pub struct Runtime {
    /// 运行时 ID
    pub id: u32,
    /// 运行时名称
    pub name: NullStr,
    /// 函数调用、对象释放是否需要线程锁
    ///
    /// 该属性决定是否延迟释放各种 Store 的内存。
    pub thread_safe: bool,

    /// 字符串的 drop 回调
    ///
    /// 如果为 None，则使用默认的 drop 回调。
    pub string_drop_callback: Option<extern "C" fn(foreign_ob: *mut std::ffi::c_void)>,

    /// 数组 drop 回调
    pub array_drop_callback: Option<extern "C" fn(object_handle: *mut std::ffi::c_void)>,

    /// 延迟释放的字符串
    pub thread_defer_dealloc_strings: boxcar::Vec<*mut std::ffi::c_void>,

    /// 延迟释放的数组
    pub thread_defer_dealloc_arrays: boxcar::Vec<*mut std::ffi::c_void>,

    /// 细粒度运行时锁，如果 thread_safe 为 false，则需要使用该锁来保护运行时
    pub lock: Arc<ReentrantMutex<()>>,
}

unsafe impl Send for Runtime {}
unsafe impl Sync for Runtime {}

impl HasPoolHandle for Runtime {
    fn fill_handle(&mut self, id: usize) {
        self.id = id as u32;
    }
}